/**
 * @file   Packet.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2013-03-11
 * @brief  DataBus Packet.
 *
 * Copyright (C) 2013 Djuro Drljaca <djurodrljaca@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DataBus/Common/Packet.h>

DataBus::Packet::Packet()
    : m_packetHeader(),
      m_packetPayload()
{
}

DataBus::Packet::Packet(const PacketHeader &packetHeader,
                        const PacketPayload &packetPayload)
    : m_packetHeader(packetHeader),
      m_packetPayload(packetPayload)
{
}

DataBus::Packet::~Packet()
{
}

DataBus::PacketHeader DataBus::Packet::getHeader() const
{
    return m_packetHeader;
}

DataBus::PacketPayload DataBus::Packet::getPayload() const
{
    return m_packetPayload;
}

quint8 DataBus::Packet::getSource() const
{
    return m_packetHeader.getSource();
}

void DataBus::Packet::setSource(const quint8 source)
{
    m_packetHeader.setSource(source);
}

quint8 DataBus::Packet::getDestination() const
{
    return m_packetHeader.getDestination();
}

void DataBus::Packet::setDestination(const quint8 destination)
{
    m_packetHeader.setDestination(destination);
}

DataBus::PacketType DataBus::Packet::getPacketType() const
{
    return m_packetHeader.getPacketType();
}

void DataBus::Packet::setPacketType(const PacketType packetType)
{
    m_packetHeader.setPacketType(packetType);
}

quint8 DataBus::Packet::getPacketId() const
{
    return m_packetHeader.getPacketId();
}

void DataBus::Packet::setPacketId(const quint8 packetId)
{
    m_packetHeader.setPacketId(packetId);
}

quint8 DataBus::Packet::getPayloadSize() const
{
    return m_packetHeader.getPayloadSize();
}

QByteArray DataBus::Packet::getData() const
{
    return m_packetPayload.getData();
}

void DataBus::Packet::setData(const QByteArray &data)
{
    m_packetHeader.setPayloadSize(data.size());
    m_packetPayload.setData(data);
}

QByteArray DataBus::Packet::toBinary() const
{
    // Create array
    QByteArray packetData;

    packetData.append('\x02');
    packetData.append(m_packetHeader.toBinary());

    if (m_packetHeader.getPayloadSize() > 0)
    {
        packetData.append(m_packetPayload.toBinary());
    }

    packetData.append('\x03');

    // Return array
    return packetData;
}
